﻿using System;
using System.Collections.Generic;

using WpfAdmin.Everything.Enums;

namespace WpfAdmin.Everything.Queryable {
    internal class DateQueryable : Queryable, IDateQueryable {
        private string searchPattern;
        internal DateQueryable(IQueryGenerator parent, string kind)
            : base(parent) {
            this.searchPattern = kind;
            FileInfoIndex fileInfoIndex;
            switch (this.searchPattern) {
                default:
                    this.Flags = RequestFlags.DateModified;
                    fileInfoIndex = FileInfoIndex.DateModified;
                    break;
                case "dc:":
                    this.Flags = RequestFlags.DateCreated;
                    fileInfoIndex = FileInfoIndex.DateCreated;
                    break;
                case "dr:":
                    this.Flags = RequestFlags.DateRun;
                    fileInfoIndex = FileInfoIndex.DateAccessed;
                    break;
                case "da:":
                    this.Flags = RequestFlags.DateAccessed;
                    fileInfoIndex = FileInfoIndex.DateAccessed;
                    break;
            }
            this.IsIndexed = EverythingWrapper.Everything_IsFileInfoIndexed(fileInfoIndex);
        }

        public IQuery After(DateTime date) {
            return this.DateSearch($">{date.ToShortDateString()}");
        }

        public IQuery After(Dates date) {
            return this.DateSearch($">{date}");
        }

        public IQuery Before(DateTime date) {
            return this.DateSearch($"<{date.ToShortDateString()}");
        }

        public IQuery Before(Dates date) {
            return this.DateSearch($"<{date}");
        }

        public IQuery Between(DateTime from, DateTime to) {
            return this.DateSearch($"{from.ToShortDateString()}-{to.ToShortDateString()}");
        }

        public IQuery Between(Dates from, Dates to) {
            return this.DateSearch($"{from}-{to}");
        }

        public IQuery Equal(DateTime date) {
            return this.DateSearch($"{date.ToShortDateString()}");
        }

        public IQuery Equal(Dates date) {
            return this.DateSearch($"{date}");
        }

        public IQuery Last(int count, CountableDates countableDate) {
            return this.DateSearch($"last{count}{countableDate}");
        }

        public IQuery Next(int count, CountableDates countableDate) {
            return this.DateSearch($"next{count}{countableDate}");
        }

        private IQuery DateSearch(string pattern) {
            this.searchPattern += pattern;
            return new Query(this);
        }

        public override IEnumerable<string> GetQueryParts() {
            foreach (var queryPart in base.GetQueryParts()) {
                yield return queryPart;
            }
            yield return this.searchPattern;
        }
    }
}
